package com.budwk.app.sqfwV2.generator.dayingRoadPdf;

import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfw.constants.DictConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BaseTemplatePdfGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskParamDataDTO;
import com.budwk.app.util.poiUtils.PictureUtils;
import com.budwk.starter.common.result.ResultCode;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;

/**
 * 食品监督协管员日常检查记录表，版本号1.0
 */
public class FoodSupInsRecordPdfGenerator extends BaseTemplatePdfGenerator{

    /**
    * 判断使用使用这个generator生成pdf
    *
    * @param pdfDTO 创建pdf数据素材
    * @return 结果
    */
    @Override
    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        AbstractCustomerPdfCreateField field = pdfDTO.getAbstractCustomerPdfCreateField();
        if (field == null) {
            throw new AppException(ResultCode.NULL_DATA_ERROR.getMsg());
        }
        if (field instanceof FireSafetyOnDayingRoadPdfDTO) {
            FireSafetyOnDayingRoadPdfDTO roadPdfDTO = (FireSafetyOnDayingRoadPdfDTO) field;
            if (roadPdfDTO.getWhetherStapleFood()) {
                return true;
            }
            String mainBusinessScope = roadPdfDTO.getMainBusinessScope();
            if (StringUtils.isEmpty(mainBusinessScope)) {
                return false;
            }
            return DictConstants.MAIN_BUSINESS_SCOPE_RESTAURANT.equals(mainBusinessScope);
        }
        return false;
    }

    @Override
    public String getTemplateName() {
        return "sqfwV2/daYingLu/foodSupInsRecord.docx";
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, SqfwPdfCreateCommonBO pdfDTO) {
        // 针对特殊字段进行数据填充 进行复杂的业务逻辑
        FireSafetyOnDayingRoadPdfDTO roadPdfDTO = DayingRoadPdfComponent.getFireSafetyOnDayingRoadPdfDTO(paramMap, pdfDTO);
        paramMap.put("district", DayingRoadPdfComponent.getDistrict(pdfDTO.getBasePdfParamDTO().getDistrict()));
        // 获取是否经营副食
        String mainBusinessScope = roadPdfDTO.getMainBusinessScope();
        paramMap.put("isRestaurant", false);
        if (StringUtils.isNotEmpty(mainBusinessScope)) {
            paramMap.put("isRestaurant", DictConstants.MAIN_BUSINESS_SCOPE_RESTAURANT.equals(mainBusinessScope));
        }
        // 食品监督检查记录存储map
        List<SafeCheckTaskParamDataDTO> foods = roadPdfDTO.getFoodSupervisionRecords();
        for (SafeCheckTaskParamDataDTO problem : foods) {
            paramMap.put(problem.getFields(), DayingRoadPdfComponent.getProblem(problem));
        }
        // 3 构建巡查人员
        DayingRoadPdfComponent.buildPeersSign(pdfDTO, roadPdfDTO, paramMap);
    }

    @Override
    public ByteArrayOutputStream generatePDFAfter(XWPFTemplate xwpfTemplate, Sqfw_formSummary_new formSummary) {
        if (!DayingRoadPdfComponent.isDayYingStreet(formSummary.getUnitName())) {
            return null;
        }
        ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
        if (xwpfTemplate != null) {
            NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
            try {
                xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "协管员签字", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                        65, -53, 115);
                xwpfDocument.write(wordOutPutStream);
                return wordOutPutStream;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                PictureUtils.close(xwpfTemplate, xwpfDocument);
            }
        }

        return wordOutPutStream;
    }
}
